<template>
   <el-card>
    <div slot="header" class="clearfix">
      <my-breadcrumb>发布文章</my-breadcrumb>
    </div>
    <el-form label-width="80px" res="form" :rules="rules" :model="form">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="form.content" :options='editorOption'
        ></quill-editor>
      </el-form-item>
      <el-form-item label="封面">
        <el-radio-group v-model="form.cover">
          <el-radio label="单图"></el-radio>
          <el-radio label="三图"></el-radio>
          <el-radio label="无图"></el-radio>
          <el-radio label="自动"></el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- 频道组件 -->
      <my-channel v-model="form.channelId"></my-channel>
      <el-form-item>
        <el-button type="primary" @click="hAdd(false)">发布</el-button>
        <el-button @click="hAdd(true)">存入草稿</el-button>
      </el-form-item>
    </el-form>

  </el-card>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'AddArticle',
  components: {
    quillEditor
  },
  data () {
    return {
      form: {
        title: '',
        content: '',
        cover: ''
      },
      // 验证规则配置对象
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在5到30之间', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      },
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
        // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            ['image']
          ]
        }
      }
    }
  },
  methods: {
    async hPublish (draft = false) {
      // 1.验证数据有效性
      try {
        const { title, content, channelId } = this.form
        // 调用接口实现添加
        const res = await this.$http({
          method: 'post',
          url: '/mp/v1_0/articles',
          params: {
            draft
          },
          data: {
            title,
            content,
            channel_id: channelId,
            cover: {
              type: 0, // 先做无图的情况
              images: []
            }
          }
        })
        console.log(res)
        this.$message.success('发布成功了')
      } catch (err) {
        console.log(err)
        this.$message.error('发布失败')
      }
    },
    // 拼接参数
    buildParams () {
      const { title, content, channelId } = this.form
      const paramsObj = {
        title: title,
        content: content,
        cover: {
          type: 0,
          images: []
        },
        channel_id: channelId
      }
      return paramsObj
    },
    // 发布文章
    hAdd (isDraft) {
      // 收集参数
      const paramsObj = this.buildParams()
      // 2.按接口文档要求,去发ajax
      this.$axios({
        method: 'POST',
        url: '/mp/v1_0/articles?draft=' + isDraft,
        data: paramsObj
      }).then(res => {
        this.$message.success('添加文章成功')
        console.log(res)
      }).catch(err => {
        console.log(err)
        this.$message.error('添加文章失败11')
      })
    }
  }
}
</script>

<style scoped lang='less'>
 /* 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能 */
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
